package com.javalong.affairs.module.renwujinzhang.activity;

import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.base.common.ViewHolder;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.listener.DefaultResponseListener;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.javalong.affairs.module.renwujinzhang.model.Task;
import com.javalong.affairs.module.renwujinzhang.ui.ImageEditView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectTimeView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectUserView;
import com.javalong.affairs.module.renwujinzhang.ui.UIValue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewNodeActivity extends RecycleListActivity {

    private int nodeCount = 1;
    private Task task;

    private List<ViewGroup> groups;

    public static void toThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, NewNodeActivity.class);
        context.startActivity(intent);
    }

    public static void toThisActivity(Context context, Task task) {
        Intent intent = new Intent();
        intent.setClass(context, NewNodeActivity.class);
        intent.putExtra(BundleKey.TASK, task);
        context.startActivity(intent);
    }

    @Override
    public void afterInit() {
        groups = new ArrayList<>();
        getSupportActionBar().setTitle("节点计划");
        task = (Task) getIntent().getSerializableExtra(BundleKey.TASK);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewGroup viewGroup = (ViewGroup) mInflater.inflate(R.layout.item_plannode, parent, false);
        groups.add(viewGroup);
        return new ViewHolder(viewGroup);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ImageEditView ie_content = holder.getView(R.id.ie_content);
        ImageEditView ie_title = holder.getView(R.id.ie_title);
        TextView tv_node = holder.getView(R.id.tv_node);
        SelectUserView suv_user = holder.getView(R.id.suv_user);
        suv_user.setHintText("选择协办人");
        SelectTimeView stv_start = holder.getView(R.id.stv_start);
        stv_start.setHintText("开始时间");
        SelectTimeView stv_end = holder.getView(R.id.stv_end);
        stv_end.setHintText("结束时间");
        tv_node.setText("节点 " + position);
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return nodeCount;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_addnode, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add:
                nodeCount++;
                getRecyclerView().getAdapter().notifyItemInserted(nodeCount - 1);
                break;
            case R.id.action_assign:
                save();
                break;
        }
        return true;
    }

    /**
     * 保存
     */
    private void save() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put(RequestKey.ID, task.getId());
        for (int i = 0; i < nodeCount; i++) {
            String title = (String) getValue(i, 1);
            String content = (String) getValue(i, 2);
            String startTime = (String) getValue(i, 3);
            String endTime = (String) getValue(i, 4);
            String users = (String) getValue(i, 5);

            param.put("content[" + i + "]", content);
            param.put("title[" + i + "]", title);

            param.put("receiveId[" + i + "][]", users);

            param.put("timeBegin[" + i + "]", startTime + "");
            param.put("timeEnd[" + i + "]", endTime + "");
        }
        String url = UrlUtils.getUrl("Task", "action_split_save");
        HttpUtils.post(url, param, new DefaultResponseListener() {
            @Override
            public void onResponse(String jsonStr) {
                super.onResponse(jsonStr);
                //ToastUtils.showMessage("保存成功");
                submit();
            }
        });
    }

    /**
     * 确认分配
     */
    private void submit() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put(RequestKey.ID, task.getId());
        for (int i = 0; i < nodeCount; i++) {
            String title = (String) getValue(i, 1);
            String content = (String) getValue(i, 2);
            String startTime = (String) getValue(i, 3);
            String endTime = (String) getValue(i, 4);
            String users = (String) getValue(i, 5);

            param.put("content[" + i + "]", content);
            param.put("title[" + i + "]", title);

            param.put("receiveId[" + i + "][]", users);

            param.put("timeBegin[" + i + "]", startTime + "");
            param.put("timeEnd[" + i + "]", endTime + "");
        }
        String url = UrlUtils.getUrl("Task", "action_split_send");
        HttpUtils.post(url, param, new DefaultResponseListener() {
            @Override
            public void onResponse(String jsonStr) {
                super.onResponse(jsonStr);
                finish();
            }
        });
    }

    private Object getValue(int index, int childIndex) {
        ViewGroup viewGroup = groups.get(index);
        UIValue uiValue = (UIValue) viewGroup.getChildAt(childIndex);
        return uiValue.getValue();
    }

}
